<?php

namespace Admin\Controller;

use Zend\Paginator\Paginator;
use Admin\View\Helper;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Helper\HeadTitle;
use Admin\Form\SongForm;
use Admin\Model\Song;
use Admin\Controller\MyController;

class SongController extends MyController
{
	protected $songTable;

	public function init() {
		parent::init();
		$viewHelper = new Helper($this);	
	}
	
	public function indexAction()
	{   
		self::init();
		$layout = $this->layout();
		$layout->header = array(
				'title'=>'Song / liste',
				'items'=>array(
						array('url'=>$this->url()->fromRoute('song', array('action'=>'add')), 'title'=>'Ajout'),
				),
				'show'=>true,
		);
		$headtitle = new HeadTitle();
		$headtitle->append($layout->header['title']);
		$config = $this->getServiceLocator()->get('Config');
		Paginator::setDefaultItemCountPerPage($config['paginator']['defaultItemCountPerPage']);
		$rows = $this->getSongTable()->getPage(array('page'=>$this->getRequest()->getQuery('page', 1), 'user_id'=>$this->authUser->getId()));
		
		$data = array(
				'rows' => $rows,
		);
		
		$viewModel = new ViewModel($data);
		return $viewModel;
	}

	public function editAction()
	{
		self::init();
		$layout = $this->layout();
		$layout->header = array(
				'title'=>'Song / Modification',
				'items'=>array(
						array('url'=>$this->url()->fromRoute('song', array('action'=>'add')), 'title'=>'Ajout'),
						array('url'=>$this->url()->fromRoute('song', array('action'=>'index')), 'title'=>'Liste'),
				'show'=>true,
				)
		);
		$headtitle = new HeadTitle();
		$headtitle->append($layout->header['title']);

		$id = (int) $this->getRequest()->getPost('id', $this->getRequest()->getQuery('id', 0));
		if (!$id) {
			return $this->redirect()->toRoute('song', array(
					'action' => 'add'
			));
		}
		$song = $this->getSongTable()->getSong($id);
		$form  = new SongForm();
		$form->bind($song);
		$request = $this->getRequest();
		if ($request->isPost()) {
			$form->setInputFilter($song->getInputFilter());
			$form->setData($request->getPost());
			if ($form->isValid()) {
				$this->getSongTable()->saveSong($song);
				if($request->getPost('submitquit')) {
					return $this->redirect()->toRoute('song');
				} else {
					return array('id' => $id, 'form' => $form, 'flashmsg' => array('text' => 'Enregistrement bien effectué', 'type' => 'success'));
				}
			} else {
				return array('id' => $id, 'form' => $form, 'flashmsg' => array('text' => 'Erreur ! Certains champs sont mal remplis', 'type' => 'error'));
			}
		}
		return array('id' => $id, 'form' => $form);
			
	}

	public function addAction() 
	{
		self::init();
		$layout = $this->layout();
		$layout->header = array(
				'title'=>'Song / Ajout',
				'items'=>array(
						array('url'=>$this->url()->fromRoute('song', array('action'=>'index')), 'title'=>'Liste'),
				'show'=>true,
				)
		);
		$headtitle = new HeadTitle();
		$headtitle->append($layout->header['title']);

		$form = new SongForm();
		$form->get('user_id')->setValue($this->authUser->getId());
		$request = $this->getRequest();
		if ($request->isPost()) {
			$song = new Song();
			$form->setInputFilter($song->getInputFilter());
			$form->setData($request->getPost());
			if ($form->isValid()) {
				$song->exchangeArray($form->getData());
				$this->getSongTable()->saveSong($song);
				return $this->redirect()->toRoute('song');
			} else {
				return array('form' => $form, 'flashmsg' => array('text' => 'Erreur ! Certains champs sont mal remplis', 'type' => 'error'));
			}
		}
		return array('form' => $form);
	}

	public function deleteAction() 
	{
		self::init();
		$layout = $this->layout();
		$layout->title = 'Song / Suppression';
		$headtitle = new HeadTitle();
		$headtitle->append($layout->title);
		$id = (int) $this->getRequest()->getPost('id', 0);
		if ($id) {
			$this->getSongTable()->deleteSong($id);
		}
		return $this->redirect()->toRoute('song');
	}


	public function getSongTable() {
		if(!$this->songTable) {
			$sm = $this->getServiceLocator();
			$this->songTable = $sm->get('Admin\Model\SongTable');
		}
		return $this->songTable;
	}

}